﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LAVAA.WEB.MvcHelpers
{
    public static class CustomHelpers
    {
        /// <summary>
        /// Creates a link that will open a jQuery UI dialog form.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText">The inner text of the anchor element</param>
        /// <param name="dialogContentUrl">The url that will return the content to be loaded into the dialog window</param>
        /// <param name="dialogId">The id of the div element used for the dialog window</param>
        /// <param name="dialogTitle">The title to be displayed in the dialog window</param>
        /// <param name="updateUrl">The url that will return the content to be loaded into the traget div</param>
        /// <returns></returns>
        public static MvcHtmlString DialogFormLink(this HtmlHelper htmlHelper, string linkText, string dialogContentUrl,
             string dialogTitle, string updateUrl)
        {
            TagBuilder builder = new TagBuilder("a");
            builder.SetInnerText(linkText);
            builder.Attributes.Add("href", dialogContentUrl);
            builder.Attributes.Add("data-dialog-title", dialogTitle);
            builder.Attributes.Add("data-update-url", updateUrl);

            // Add a css class named dialogLink that will be
            // used to identify the anchor tag and to wire up
            // the jQuery functions
            builder.AddCssClass("dialogLink");

            return new MvcHtmlString(builder.ToString());
        }

        public static MvcHtmlString DialogFormLinkWithData(this HtmlHelper htmlHelper, string linkText, string dialogContentUrl,
            string dialogTitle, string updateUrl, string id)
        {
            TagBuilder builder = new TagBuilder("a");
            builder.SetInnerText(linkText);
            builder.Attributes.Add("href", dialogContentUrl);
            builder.Attributes.Add("data-dialog-title", dialogTitle);

            builder.Attributes.Add("data-update-url", updateUrl);
            builder.Attributes.Add("data-id", id);

            // Add a css class named dialogLink that will be
            // used to identify the anchor tag and to wire up
            // the jQuery functions
            builder.AddCssClass("dialogLink");

            return new MvcHtmlString(builder.ToString());
        }


        public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string username, string linkText, string url, string group)
        {
            CustomRoleProvider customRoleProvider = new CustomRoleProvider();
            if (!customRoleProvider.GetRolesForUser(username).Contains(group))
                return new MvcHtmlString("");
            return MenuLink(htmlHelper, username, linkText, url);
        }

        public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string username, string linkText, string url)
        {
            TagBuilder builder = new TagBuilder("li");
            TagBuilder builder2 = new TagBuilder("a");

            builder2.SetInnerText(linkText);
            builder2.Attributes.Add("href", url);
            builder.InnerHtml = builder2.ToString();

            return new MvcHtmlString(builder.ToString());
        }
    }
}